package ar.com.psicosoft.persistence.psicosoft;

import java.io.Serializable;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import ar.com.psicosoft.exception.PsicosoftException;
import ar.com.psicosoft.model.psicosoft.EstadoCivil;

@Repository(value = "estadoCivilDao")
public class EstadoCivilDaoImpl implements EstadoCivilDao, Serializable {

	private static final long serialVersionUID = 1L;
	@PersistenceContext(unitName = "psicosoftPU")
	private EntityManager entityManager;

	@SuppressWarnings("unchecked")
	@Override
	public EstadoCivil buscarEstadoCivilPorId(Integer idEstadoCivil)
			throws PsicosoftException {
		Query query = this.entityManager
				.createNamedQuery("EstadoCivil.buscarPorId");
		query.setParameter("idEstadoCivil", idEstadoCivil);
		try {
			List<EstadoCivil> resultList = query.getResultList();
			if (!resultList.isEmpty()) {
				// ignores multiple results
				return resultList.get(0);
			} else {
				return null;
			}
		} catch (PersistenceException pe) {
			throw new PsicosoftException(
					"Error al buscar estadoCivil en la Base de Datos");
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public EstadoCivil buscarEstadoCivilPorNombreEstadoCivil(
			String nombreEstadoCivil) throws PsicosoftException {
		Query query = this.entityManager
				.createNamedQuery("EstadoCivil.buscarPorNombreEstadoCivil");
		query.setParameter("nombreEstadoCivil", nombreEstadoCivil);
		try {
			List<EstadoCivil> resultList = query.getResultList();
			if (!resultList.isEmpty()) {
				// ignores multiple results
				return resultList.get(0);
			} else {
				return null;
			}
		} catch (PersistenceException pe) {
			throw new PsicosoftException(
					"Error al buscar estadoCivil en la Base de Datos");
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<EstadoCivil> buscarEstadosCiviles() throws PsicosoftException {
		Query query = this.entityManager
				.createNamedQuery("EstadoCivil.buscarEstadosCiviles");
		try {
			List<EstadoCivil> resultList = query.getResultList();
			return resultList;
		} catch (PersistenceException pe) {
			throw new PsicosoftException(
					"Error al buscar estados Civiles en la Base de Datos");
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = PsicosoftException.class)
	public void guardarEstadoCivil(EstadoCivil estadoCivil)
			throws PsicosoftException {
		try {
			this.entityManager.persist(estadoCivil);
			this.entityManager.flush();
			this.entityManager.refresh(estadoCivil);
		} catch (PersistenceException pe) {
			throw new PsicosoftException(
					"Error al insertar estadoCivil en la Base de Datos");
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = PsicosoftException.class)
	public void modificarEstadoCivil(EstadoCivil estadoCivil)
			throws PsicosoftException {
		try {
			this.entityManager.merge(estadoCivil);
			this.entityManager.flush();
		} catch (PersistenceException pe) {
			throw new PsicosoftException(
					"Error al modificar estadoCivil en la Base de Datos");
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = PsicosoftException.class)
	public void eliminarEstadoCivil(EstadoCivil estadoCivil)
			throws PsicosoftException {
		try {
			this.entityManager.remove(this.entityManager.merge(estadoCivil));
		} catch (PersistenceException pe) {
			throw new PsicosoftException(
					"Error al eliminar estadoCivil en la Base de Datos");
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = PsicosoftException.class)
	public void bajaEstadoCivil(EstadoCivil estadoCivil)
			throws PsicosoftException {
		try {
			this.entityManager.merge(estadoCivil);
			this.entityManager.flush();
		} catch (PersistenceException pe) {
			throw new PsicosoftException(
					"Error al modificar estadoCivil en la Base de Datos");
		}
	}

}